package com.zzw.controller;

import com.zzw.service.RedisCacheService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Redis测试控制器
 * 提供Redis测试接口
 */
@Controller
@RequestMapping("/api/redis")
public class RedisTestController {

    @Autowired
    private RedisCacheService redisCacheService;
    
    /**
     * 设置字符串缓存
     * @param key 缓存键
     * @param value 缓存值
     * @return 操作结果
     */
    @PostMapping("/set")
    @ResponseBody
    public Map<String, Object> setCache(
            @RequestParam String key,
            @RequestParam String value,
            @RequestParam(required = false, defaultValue = "1800") Long timeout) {
        
        Map<String, Object> result = new HashMap<>();
        try {
            redisCacheService.setCache(key, value, timeout);
            result.put("success", true);
            result.put("message", "缓存设置成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "缓存设置失败：" + e.getMessage());
        }
        return result;
    }
    
    /**
     * 获取缓存
     * @param key 缓存键
     * @return 缓存值
     */
    @GetMapping("/get")
    @ResponseBody
    public Map<String, Object> getCache(@RequestParam String key) {
        Map<String, Object> result = new HashMap<>();
        try {
            Object value = redisCacheService.getCache(key);
            result.put("success", true);
            result.put("data", value);
            result.put("exists", value != null);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "获取缓存失败：" + e.getMessage());
        }
        return result;
    }
    
    /**
     * 删除缓存
     * @param key 缓存键
     * @return 操作结果
     */
    @DeleteMapping("/delete")
    @ResponseBody
    public Map<String, Object> deleteCache(@RequestParam String key) {
        Map<String, Object> result = new HashMap<>();
        try {
            redisCacheService.deleteCache(key);
            result.put("success", true);
            result.put("message", "缓存删除成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "缓存删除失败：" + e.getMessage());
        }
        return result;
    }
    
    /**
     * 检查缓存是否存在
     * @param key 缓存键
     * @return 检查结果
     */
    @GetMapping("/exists")
    @ResponseBody
    public Map<String, Object> existsCache(@RequestParam String key) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean exists = redisCacheService.hasKey(key);
            result.put("success", true);
            result.put("exists", exists);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "检查缓存失败：" + e.getMessage());
        }
        return result;
    }
    
    /**
     * 刷新缓存过期时间
     * @param key 缓存键
     * @return 操作结果
     */
    @PostMapping("/refresh")
    @ResponseBody
    public Map<String, Object> refreshCache(
            @RequestParam String key,
            @RequestParam(required = false, defaultValue = "1800") Long timeout) {
        
        Map<String, Object> result = new HashMap<>();
        try {
            redisCacheService.refreshExpire(key, timeout);
            result.put("success", true);
            result.put("message", "缓存过期时间刷新成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "缓存过期时间刷新失败：" + e.getMessage());
        }
        return result;
    }
    
    /**
     * 测试Shiro会话和Redis的集成
     * 在会话中存储测试数据，并检查是否成功保存到Redis
     * @return 测试结果
     */
    @GetMapping("/test-shiro-session")
    @ResponseBody
    public Map<String, Object> testShiroSession() {
        Map<String, Object> result = new HashMap<>();
        try {
            // 获取当前Subject和Session
            Subject subject = SecurityUtils.getSubject();
            Session session = subject.getSession();
            
            // 生成随机测试值
            String testValue = "test-value-" + System.currentTimeMillis();
            
            // 存储到会话中
            session.setAttribute("testKey", testValue);
            
            // 从会话中重新获取
            Object retrievedValue = session.getAttribute("testKey");
            
            // 检查是否保存成功
            boolean success = testValue.equals(retrievedValue);
            
            result.put("success", success);
            result.put("sessionId", session.getId());
            result.put("originalValue", testValue);
            result.put("retrievedValue", retrievedValue);
            result.put("message", success ? "会话数据成功存储到Redis" : "会话数据存储失败");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "测试失败：" + e.getMessage());
            result.put("error", e.toString());
        }
        return result;
    }
} 